<?php
class Category{

	function __construct(){
	}

	function __destruct(){
	}
	
	/* This function is used to get all main categories
	@Input : interger $id
	@Output : array $categories
	*/
	public function getMainCategories($id = 0){
		$where = " WHERE c.parent_id = 0 ";
		if(!empty($id)) $where.=" AND c.id =".$id;
		$where.=" AND c.status != '2' ";
		$qry = "SELECT * FROM category as c ".$where." ORDER BY c.id DESC";
		$rs = MyDB::$db->Execute($qry);
		$categories = array();
		while ($row = $rs->fetchRow()) {
			$categories[] = $row;
		}
		return $categories;
	}
	
	/* This function is used to get all main categories
	@Input : interger $id
	@Output : array $categories
	*/
	public function getSubCategories($parent_id = 0, $flag = ''){
		//if(!empty($id)) $where = " where v.id ";
		$where = " WHERE depth = 1 ";
		if($parent_id != 0 && $flag == ''){
			$where.=" AND c.parent_id = ".$parent_id;
		}else if($parent_id != 0 && $flag != ''){
			$where.=" AND c.id = ".$parent_id;
		}
		$where.=" AND c.status != '2' ";
		$qry = "SELECT * FROM category as c ".$where." ORDER BY c.id DESC";
		$rs = MyDB::$db->Execute($qry);
		$subcategories = array();
		while ($row = $rs->fetchRow()) {
			$subcategories[] = $row;
		}
		return $subcategories;
	}
	
	/* This function is used to get all main categories
	@Input : interger $id
	@Output : array $categories
	*/
	public function getSubSubCategories($parent_id = 0, $flag = ''){
		//if(!empty($id)) $where = " where v.id ";
		$where = " WHERE depth = 2 ";
		if($parent_id != 0 && $flag == ''){
			$where.=" AND c.parent_id = ".$parent_id;
		}else if($parent_id != 0 && $flag != ''){
			$where.=" AND c.id = ".$parent_id;
		}
		$where.=" AND c.status != '2' ";
		//$where = " WHERE c.parent_id = ".$parent_id." AND depth = 2 AND c.status != '2' ";
		$qry = "SELECT * FROM category as c ".$where." ORDER BY c.id DESC";
		$rs = MyDB::$db->Execute($qry);
		$subsubcategories = array();
		while ($row = $rs->fetchRow()) {
			$subsubcategories[] = $row;
		}
		return $subsubcategories;
	}
	
	/* This function is used to get all categories
	@Input : interger $id
	@Output : array $categories
	*/
	public function getCategories($id = NULL){
		//if(!empty($id)) $where = " where v.id ";
		$where = " WHERE c.status != '2' ";
		$qry = "SELECT * FROM category as c ".$where." ORDER BY c.id DESC";
		$rs = MyDB::$db->Execute($qry);
		$categories = array();
		while ($row = $rs->fetchRow()) {
			$categories[] = $row;
		}
		return $categories;
	}
	
	public function getParentId($id=''){
		$qry = "SELECT parent_id FROM category as c WHERE c.id= ".$id;
		$rs = MyDB::$db->Execute($qry);
		$arrParentId = array();
		while ($row = $rs->fetchRow()) {
			$arrParentId[] = $row;
		}
		return $arrParentId[0]['parent_id'];
	}
	
	public function getIdFromParentId($parent_id=''){
		$qry = "SELECT id FROM category as c WHERE c.parent_id= ".$parent_id;
		$rs = MyDB::$db->Execute($qry);
		$arrId = array();
		while ($row = $rs->fetchRow()) {
			$arrId[] = $row;
		}
		return $arrId;
	}
	
	public function updateSubSubCategoryStatus($parent_id='', $status=''){
		$arrId = self::getIdFromParentId($parent_id);
		if(!empty($arrId)){
			$arrIds = array();
			foreach($arrId as $key => $val){
				$arrIds[] =  $val['id'];
			}
			$ids = implode(',',$arrIds);
			$qry = "UPDATE category SET status = ".MyDB::$db->qstr($status)." WHERE parent_id IN(".$ids.")";
			$rs = MyDB::$db->Execute($qry);
		}
	}
	
	public function getServiceAttributesNew($id=NULL){
		if($id)$where.=" AND vpa.service_id=".$id;
		$qry = "SELECT vpa.*, am.title
		FROM services_attributes vpa, attribute_master am where vpa.attribute_id=am.id  ".$where;
		$rs = MyDB::$db->Execute($qry);
		$product_attributes = array();
		while ($row = $rs->fetchRow()) {
			$rowUnit = CommonUtility::getDataById($row['unit_id'], 'unit_master');
			if( $rowUnit ){
				$row['unit'] = $rowUnit['title'];
			}
			$row['attribute_name'] = $row['title'];
			$product_attributes[] = $row;
		}
		return $product_attributes;
	}
}	
?>